perm filename A39[106,RWF] blob
sn#774622 filedate 1984-11-01 generic text, type C, neo UTF8
COMMENT ā VALID 00002 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 Files (I)
C00014 ENDMK
Cā;
Files (I)
A _file_ is a collection of information in sequential form for use by a
computer. For the moment, we will be concerned only with _text_ files:
files made up of symbols that appear on the keys of typewriters and computer
terminals.
A text file can be produced by a Luman at a computer terminal. By `reading'
such a text file, a computer can `see' what the human has typed. In this
book, we assume that all computer programs are first designed on paper,
then typed into a machine that makes a text file. That machine could be a
_terminal_ keyboard (one directly attached to a computer), or it could be
a machine which independently records a text file on a magnetic or paper tape,
punched cards, etc. We also assume that most data provided to computer
programs is called an input file prepared in advance in an (input) text file,
for reading by a program.
The results computed by a program can be saved in the computers `memory'
for use by other programs printed directly on paper, or displayed on a
cathode-ray (TV-type) display device for immediate visual inspection.
Most often, though, they are made into text files from which all the above
can be done. The use of such (_output_) files separates the design of a
program from consideration of the uses to be made of its results.
In Pascal, text files have two kinds of names. Each file may hve an _external_
name, by which it is known to the world at large, and an _internal_ name,
by which it is known to a particular program. This is akin to the legal
convention by which standard legal documents refer to me as `Seller',
`Lessor', `Party of the First Part', or `John Doe', while outside these documents
I am Robert W. Floyd. The merit of this two-named convention is that a
program, like a standard lease form, can be used unchanged on different
occasions to deal with different entities: text files by the program,
renters by the lease form.
A Pascal program is prepared as a text file. Then, by a process which is not
standardized but varies among computers, it is translated into machine
language and loaded into the machine's memory ready for execution. Before
the algorithm embodied in the program is executed, however, each internal
file name is given an external name of an actual file. The computer may do
this by printing the internal name and waiting for a user to type the
corresponding external name. In our first example programs, the only
internal name is 'OUTPUT'; if the external name is SAMPLE.OUT, the exchange is
\tt OUTPUT: _SAMPLE.OUT cr_
where underlined material is typed by the human user and cr means the CARRIAGE
RETURN key.
The overall pattern of programming in Pascal, in its simplest form, is:
(1) Write a program.
(2) By typing, make it into a text file, giving it a name.
(3) Write down any data needed by the program.
(4) Type the data into a text file, giving it an external name.
(5) Choose external names for any output file to be written by the
program. If required, create an empty file of that name.
(6) Cause translation of the program into machine language.
(7) Cause loading of the translated program into machine memory.
(8) Initiate execution of the loaded program. (Steps (6), (7), and (8)
are often combined.)
(9) Provide an external name for every internal name used by the program
(input and output files, _not_ the file containing the program).
(10) Wait while the program carries out its algorithm.
(11) Inspect, print, or otherwise use the results in the output file.
Inside a Pascal program, a number N can be _written_ to (i.e. added at
the end of) a text file by the command
WRITE(N)
where N may be a constant, a predefined variable, or an _expression_
(formula) made up from variables and constants by the use of arithmetic
and functions.
Examples:
Command What it writes
WRITE(1984) 1984
WRITE(0001984) 1984
WRITE(1984-1948) 36
WRITE(1984+X) 2001 (if X is 17)
WRITE(6*7) 42 (`*' stands for multiplication)
Numbers that do or may contain decimal points are written in a form of
scientific notation.
WRITE(1000/3) 3.333333E+02
meaning 3.333333x10ā{+2}, or 333.3333.
For the moment, we won't worry about which form a number is printed in.
WRITE(3*5) 15
WRITE(3.0*5) 1.500000E+01
If only numbers are written on an output file, the file will eventually
become longer than what can be printed on a line. If a program writes
1001 1002 1003 ... 1026 1027 ...
and the resulting file is printed on paper 132 characters wide, only
1001 1002 1003 ... 1026 10
can be printed on the first line. The rest is typically lost, or broken
in the middle and continued on the next line as if it were ... 1026 1027...
Both are unsatisfactory. The command
WRITELN
puts a carriage return symbol or equivalent in a file:
WRITE(1001);
WRITE(1002);
.
.
.
WRITE(1026);
WRITELN;
WRITE(1027);
etc,
produces the file
1001 1002 1003 ... 1026 cr 1027...
which, when printed, gives
1001 1002 1003 ... 1026
1027 ...
A complete program to do the above is
PROGRAM MI(OUTPUT);
BEGIN
WRITE(1001); (the semicolon is essential)
WRITE(1002);
.
.
.
WRITE(1026);
WRITELN;
WRITE(1027);
.
.
.
WRITE(1052) (but no semicolon here)
END. (the period is essential)
The general form of a program is given by these definitions:
(1) A program is a program heading, a block, and a period.
(2) A program heading is `PROGRAM', an internal name for the program,
`(', internal names for files separated by commas, `);'
(3) A block is a declaration part (not needed here, so we defer explanation)
followed by a compound command.
(4) A compound command is `BEGIN', one or more commands with semicolons
between them, 'END'.
These definitions are usually shown diagrammatically:
Program:
program block
heading
Program heading:
PROGRAM internal
name
Block:
declaration compound
part command
Compound command:
BEGIN Command END
In the diagrams above, any path you take following the arrows gives a
grammatically correct way of composing a program or fragment of program.
For example, a compound command can be anything of the form
BEGIN S1;S2 END
provided S1 and S2 are commands. Naturally, a program can be grammatically
right (and therefore translatable) without being correct or even meaningful.
The sentence ``The ferocious mouse carried the green gorilla through the
coming six weeks of sleep'' is grammatical, I could translate it into French
if I knew French, but is readily perceived as defective.
A helpful set of abbreviated forms for WRITE and WRITELN commands are:
WRITE(X,Y,Z) for WRITE(X);
WRITE(Y);
WRITE(Z);
WRITELN(X) for WRITE(X);
WRITELN;
WRITELN(X,Y,Z) for WRITE(X);
WRITE(Y);
WRITE(Z);
WRITELN;
We can compress the above program to
PROGRAM MI (OUTPUT);
BEGIN
WRITELN(1001,1002,...,1026);
WRITE(1027,1028,...,1052)
END.
(There is a still better way)